package cn.stronglink.assetmanage.business.inventory.presenter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.MainActivity;
import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.model.InventoryCountModel;
import cn.stronglink.assetmanage.business.inventory.model.InventoryModel;
import cn.stronglink.assetmanage.business.inventory.model.InventorySyncModel;
import cn.stronglink.assetmanage.business.inventory.view.count.InventoryCountView;
import cn.stronglink.assetmanage.business.inventory.view.sync.InventorySyncFragment;
import cn.stronglink.assetmanage.business.inventory.view.sync.InventorySyncView;
import cn.stronglink.assetmanage.business.task.model.ITaskSyncListener;
import cn.stronglink.assetmanage.business.task.model.OrderType;
import cn.stronglink.assetmanage.business.task.model.TaskSyncModel;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.index.entity.PromissionEntity;
import cn.stronglink.assetmanage.utils.Constants;
import okhttp3.Call;
import okhttp3.Response;

import static android.R.attr.y;

/**
 * Created by chencongcong on 2018/6/15.
 */

public class InventorySyncPresenter{
    private InventorySyncModel inventorySyncModel;
    private InventorySyncView inventorySyncView;
    //盘点表单列表
    private List<Inventory> inventoryList;
    //盘点资产二级列表
    private Map<String, List<InventoryDetail>> itemDetailList;
    //选择的表单
    private Inventory inventory;

    public InventorySyncPresenter(InventorySyncModel inventorySyncModel, InventorySyncView inventorySyncView) {
        this.inventorySyncModel = inventorySyncModel;
        this.inventorySyncView = inventorySyncView;
    }
    /**
     * 初始化未盘点表单列表
     */
    public void initInventoryListView() {
        inventoryList = inventorySyncModel.getDataByOrderNoDistince();
        inventorySyncView.getFragment().getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                inventorySyncView.showListView();
                inventorySyncView.setListData(inventoryList);
            }
        });
    }

    /**
     * 根据点击位置显示资产列表
     *
     * @param position
     */
    public void showOrderByPosition(int position) {
//        inventory = inventoryList.get(position);
//        inventorySyncView.showAssetView();
//        itemDetailList = inventorySyncModel.getDataByOrderNo(inventory.getId_());
//        inventorySyncView.setAssetData(itemDetailList);

        MainActivity activity = (MainActivity) this.inventorySyncView.getFragment().getActivity();
        PromissionEntity pe = new PromissionEntity();
        pe.setPosition(MainActivity.FRAGMENT_INVENTORY_DETAIL_ID);

        // 设置打开盘点详情后传递的参数
        inventory = inventoryList.get(position);
        Bundle bundle=new Bundle();
        bundle.putSerializable(Constants.INVENTORY_DETAIL_NO, inventory.getId_());
        pe.setBundle(bundle);

        activity.onEventRegister(pe);
    }

    public void showDetailByPosition(String groupName, int childPosition) {
        InventoryDetail inventoryDetail = itemDetailList.get(groupName).get(childPosition);
        inventorySyncView.showOrderDetail(inventoryDetail);
    }

    public void syncInventoryData() {
        inventoryList = inventorySyncModel.getInventorySyncData();
        TaskSyncModel taskSyncModel = new TaskSyncModel();
        taskSyncModel.setListener(new ITaskSyncListener() {
            @Override
            public void onFail(String orderNo, OrderType orderType, Exception e, int sum) {
                inventorySyncView.hideProgressDialog();
                inventorySyncView.showToast(orderNo+e.getMessage());
            }

            @Override
            public void onSucces(String orderNo, OrderType orderType, int sum) {
                inventorySyncView.hideProgressDialog();
                inventorySyncView.showToast("单号：["+orderNo+"]上传成功");
                initInventoryListView();
            }
        });
        if(inventoryList!=null&&inventoryList.size()>0) {
            //同步已经已经盘点数据，正常盘点和盘盈的数据调用的接口可能不一样，需要分别同步
            inventorySyncView.showProgressDialog("上传数据中...");
            taskSyncModel.uploadInventoryList(inventoryList);
        }else{
            inventorySyncView.showToast("暂无数据");
        }
//        try{
//            Gson gson = new Gson();
//            String json = gson.toJson(inventoryList);
//            final AlertDialog.Builder normalDialog =
//                    new AlertDialog.Builder(inventorySyncView.getFragment().getActivity());
//            normalDialog.setTitle("debug");
//            normalDialog.setMessage(json);
//            normalDialog.show();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }

    public void showOrderDataByOrderId(String orderNo) {
        inventory = inventorySyncModel.getInventoryByOrderNo(orderNo);
        inventorySyncView.showAssetView();
        itemDetailList = inventorySyncModel.getDataByOrderNo(orderNo);
        inventorySyncView.setAssetData(itemDetailList);
    }
}
